/*
 * @Author: your name
 * @Date: 2021-04-22 15:19:46
 * @LastEditTime: 2021-04-22 17:26:22
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \projectManageUI2.0\src\mixins\validateUtil.js
 */
export const mixin_validate = {
  data() {
    var validateForTab = (rule, value, callback) => {
      if (value === '' || value === undefined) {
        this.tabName = rule.tabname
        this.$nextTick(() => {
          var thisFormItem
          if (this.$refs.budgetForm) {
            thisFormItem = this.$refs.budgetForm.fields.filter((element) => {
              if (element.prop === rule.field) {
                return true
              }
            })
          }
          if (!thisFormItem || thisFormItem.length === 0) {
            thisFormItem = this.$refs.form.fields.filter((element) => {
              if (element.prop === rule.field) {
                return true
              }
            })
          }
          if (this.$refs.budgetForm) {
            this.$refs.budgetForm.fields.forEach((element) => {
              if (element.prop === rule.field && this.focuxItem === '' && (element.$children[1].$options._componentTag.indexOf('el-input') !== -1 || element.$children[1].$options._componentTag.indexOf('el-textarea') !== -1) && !element.$children[1].disabled) {
                this.focuxItem = element.prop
                element.$children[1].focus()
                element.$children[1].$el.scrollIntoView()
              }
            })
          }
          this.$refs.form.fields.forEach((element) => {
            if (element.prop === rule.field && this.focuxItem === '' && (element.$children[1].$options._componentTag.indexOf('el-input') !== -1 || element.$children[1].$options._componentTag.indexOf('el-textarea') !== -1) && !element.$children[1].disabled) {
              this.focuxItem = element.prop
              element.$children[1].focus()
              element.$children[1].$el.scrollIntoView()
            }
          })
          if (rule.message) {
            callback(new Error(rule.message))
          } else {
            callback(new Error(thisFormItem[0].label + "不得为空"))
          }
        })
      } else {
        callback()
      }
    }
    return {
      rulesProject: [{ validator: validateForTab, trigger: 'change', required: true }],
      focuxItem: '', // 当前获得焦点的字段编码
      modelForBudget: {
        rules: {
          costtype: {
            required: true,
            validator: validateForTab,
            message: '请选择费用类型',
            tabname: 'bugetInfo',
            trigger: 'change'
          },
          budgetmoney: {
            type: 'number',
            required: true,
            validator: validateForTab,
            message: '请输入预算金额',
            tabname: 'bugetInfo',
            trigger: 'change'
          },
          note: { required: true, tabname: 'bugetInfo', validator: validateForTab, trigger: 'change', message: '请输入预算描述' }
        },
        tableData: [
          {
            costtype: '',
            budgetmoney: undefined,
            note: ''
          }
        ]
      }
    }
  },
  props: {
    render: {
      default: 'el-dialog',
      type: String
    }
  },
  methods: {
    beforeSubmit() {
      this.focuxItem = ''
      return true
    }
  },
  mounted() {
  },
  created() {
  }
}
